/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.me.message;

import java.util.Date;

import com.cenluan.kit.UUIDKit;
import com.cenluan.user.User;
import com.jfinal.plugin.activerecord.Model;

/**
 * 留言，消息
 * 
 * @author peak
 * 
 */
public class Message extends Model<Message> {

	private static final long serialVersionUID = 1L;

	public static final Message dao = new Message();

	/**
	 * 获取接收消息的用户
	 * 
	 * @return
	 */
	public User getToUser() {
		return User.dao.findById(getStr("toUserId"));
	}

	/**
	 * 获取发送消息的用户
	 * 
	 * @return
	 */
	public User getFromUser() {
		return User.dao.findById(getStr("fromUserId"));
	}

	/**
	 * 保存留言
	 * 
	 * @param form
	 * @param to
	 * @param content
	 * @return
	 */
	public static boolean save(String form, String to, String content) {
		User.dao.findById(to).clearNewsCache();
		Message message = new Message();
		message.set("id", UUIDKit.generateUUID());
		message.set("fromUserId", form);
		message.set("toUserId", to);
		message.set("content", content);
		message.set("read", false);
		message.set("createTime", new Date());
		return message.save();
		
	}

}
